package com.ptszyxx.popose.module.main.ticket;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;

import androidx.lifecycle.ViewModelProviders;

import com.alipay.sdk.app.PayTask;
import com.ptszyxx.popose.BR;
import com.ptszyxx.popose.R;
import com.ptszyxx.popose.common.constants.BundleConstant;
import com.ptszyxx.popose.databinding.ActivityTicketDialogBinding;
import com.ptszyxx.popose.module.main.mine.MineVipFragment;
import com.ptszyxx.popose.module.main.mine.PayResult;
import com.ptszyxx.popose.module.main.ticket.vm.TicketDialogVM;
import com.ysg.base.BaseActivity;
import com.ysg.http.data.AppViewModelFactory;
import com.ysg.http.data.entity.mine.VipEntity;
import com.ysg.utils.YConfigUtil;
import com.ysg.utils.YToastUtil;

import java.util.List;
import java.util.Map;

public class TicketDialogActivity extends BaseActivity<ActivityTicketDialogBinding, TicketDialogVM> {
    public boolean showFree;

    @Override
    public int initContentView(Bundle savedInstanceState) {
        return R.layout.activity_ticket_dialog;
    }

    @Override
    public int initVariableId() {
        return BR.viewModel;
    }

    @Override
    public TicketDialogVM initViewModel() {
        AppViewModelFactory factory = AppViewModelFactory.getInstance(getApplication());
        return ViewModelProviders.of(this, factory).get(TicketDialogVM.class);
    }

    @Override
    protected void onResume() {
        super.onResume();
        if (viewModel.isJumpPay){
            viewModel.requestTicket();
        }
    }

    @Override
    public void initParam() {
        super.initParam();
        Intent intent = getIntent();
        if (intent != null){
            showFree = intent.getBooleanExtra(BundleConstant.PAY_FREE, false);
        }
    }

    @Override
    public void initData() {
        super.initData();
        viewModel.requestTicketPrice();
    }

    @Override
    public void initView() {
        super.initView();
        setFinishOnTouchOutside(false);

        Window window = getWindow();
        WindowManager.LayoutParams layoutParams = window.getAttributes();
        window.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        layoutParams.width = ViewGroup.LayoutParams.MATCH_PARENT;
        layoutParams.height = ViewGroup.LayoutParams.MATCH_PARENT;
        window.setAttributes(layoutParams);

        if (!YConfigUtil.isAlipaySwitch()){
            binding.tvAlipay.setVisibility(View.GONE);
        }
        if (!YConfigUtil.isWechatSwitch()){
            binding.tvWechat.setVisibility(View.GONE);
        }
        if (showFree){
            binding.tvFree.setVisibility(View.VISIBLE);
        }
    }

    @Override
    public void initViewObservable() {
        super.initViewObservable();
        viewModel.uc.onAlipayEvent.observe(this, o -> startAlipay());
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode==KeyEvent.KEYCODE_BACK){
            return false;
        }
        return super.onKeyDown(keyCode, event);
    }

    private Runnable payRunnable = new Runnable() {
        @Override
        public void run() {
            PayTask alipay = new PayTask(TicketDialogActivity.this);
            Map<String,String> result = alipay.payV2(viewModel.orderInfo,true);
            Message msg = new Message();
            msg.obj = result;
            mHandler.sendMessage(msg);
        }
    };

    // 必须异步调用
    private void startAlipay(){
        Thread payThread = new Thread(payRunnable);
        payThread.start();
    }

    @SuppressLint("HandlerLeak")
    private Handler mHandler = new Handler() {
        public void handleMessage(Message msg) {
            PayResult payResult = new PayResult((Map<String, String>) msg.obj);
            String resultStatus = payResult.getResultStatus();
            if (TextUtils.equals(resultStatus, "9000")) {
                YToastUtil.showShort("支付成功");
            } else {
                YToastUtil.showShort("支付失败");
            }
        }
    };
}
